package com.pps.sdk.tester.adapter;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.pps.sdk.model.DataLog;
import com.pps.sdk.tester.R;

public class DataLogAdapter extends BaseAdapter {

	private List<DataLog> dataSource = new ArrayList<DataLog>();
	private Context context;
	
	public DataLogAdapter(Context context){
		this.context = context;
	}
	
	public void setDataSource(List<DataLog> dataSource){
		if(dataSource == null){
			return ;
		}
		
		this.dataSource = dataSource;
		this.notifyDataSetChanged();
	}
	
	public void cleanData(){
		this.dataSource.clear();
		this.notifyDataSetChanged();
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return dataSource.size();
	}

	@Override
	public DataLog getItem(int arg0) {
		// TODO Auto-generated method stub
		return (arg0 < dataSource.size() && arg0 >= 0) ? dataSource.get(arg0) : null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(int position, View contentView, ViewGroup arg2) {
		// TODO Auto-generated method stub
		ViewHolde viewHolde = null;
		if(contentView == null){
			LayoutInflater inflater = LayoutInflater.from(context);
			contentView = inflater.inflate(R.layout.data_list_item, null);
			viewHolde = new ViewHolde();
			viewHolde.gameidView = (TextView) contentView.findViewById(R.id.log_gameid);
			viewHolde.activeView = (TextView) contentView.findViewById(R.id.log_active);
			viewHolde.statueView = (TextView) contentView.findViewById(R.id.log_statue);
			viewHolde.timeView = (TextView) contentView.findViewById(R.id.log_time);
			
			contentView.setTag(viewHolde);
		}else{
			viewHolde = (ViewHolde) contentView.getTag();
		}
		
		DataLog dataLog = getItem(position);
		if(dataLog != null){
			viewHolde.gameidView.setText(dataLog.gameid);
			viewHolde.activeView.setText(dataLog.action);
			viewHolde.statueView.setText(dataLog.status);
			viewHolde.timeView.setText(dataLog.postTime);
		}
		
		return contentView;
	}

	private class ViewHolde{
		public TextView gameidView;
		public TextView activeView;
		public TextView statueView;
		public TextView timeView;
	}

}
